package com.milk.Dao;

import com.milk.DB.DBConnection;
import com.milk.entity.MilkTeaDrinks;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MilkTeaDrinksDao {
    // 查询所有奶茶饮品信息
    public static List<MilkTeaDrinks> findAll() {
        List<MilkTeaDrinks> drinks = new ArrayList<>();
        String sql = "SELECT * FROM milk_tea_drinks";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql);
             ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                MilkTeaDrinks drink = new MilkTeaDrinks();
                drink.setId(resultSet.getInt("id"));
                drink.setName(resultSet.getString("name"));
                drink.setCategory(resultSet.getString("category"));
                drink.setPrice(resultSet.getDouble("price"));
                drink.setDescription(resultSet.getString("description"));
                drinks.add(drink);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return drinks;
    }

    // 根据id查询奶茶饮品信息
    public static MilkTeaDrinks findById(int id) {
        MilkTeaDrinks drink = null;
        String sql = "SELECT * FROM milk_tea_drinks WHERE id =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, id);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                drink = new MilkTeaDrinks();
                drink.setId(resultSet.getInt("id"));
                drink.setName(resultSet.getString("name"));
                drink.setCategory(resultSet.getString("category"));
                drink.setPrice(resultSet.getDouble("price"));
                drink.setDescription(resultSet.getString("description"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return drink;
    }

    // 添加奶茶饮品
    public static int add(MilkTeaDrinks milkTeaDrink) {
        String sql = "INSERT INTO milk_tea_drinks (name, category, price, description) VALUES (?,?,?,?)";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, milkTeaDrink.getName());
            statement.setString(2, milkTeaDrink.getCategory());
            statement.setDouble(3, milkTeaDrink.getPrice());
            statement.setString(4, milkTeaDrink.getDescription());
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("添加奶茶饮品失败", e);
        }
    }

    // 根据id删除奶茶饮品
    public static int deleteById(int id) {
        String sql = "DELETE FROM milk_tea_drinks WHERE id =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, id);
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    // 修改奶茶饮品信息
    public static int update(MilkTeaDrinks milkTeaDrink) {
        String sql = "UPDATE milk_tea_drinks SET name =?, category =?, price =?, description =? WHERE id =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, milkTeaDrink.getName());
            statement.setString(2, milkTeaDrink.getCategory());
            statement.setDouble(3, milkTeaDrink.getPrice());
            statement.setString(4, milkTeaDrink.getDescription());
            statement.setInt(5, milkTeaDrink.getId());
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("修改奶茶饮品信息失败", e);
        }
    }

    // 模糊搜索奶茶饮品（根据名称）
    public static List<MilkTeaDrinks> searchByName(String name) {
        List<MilkTeaDrinks> drinks = new ArrayList<>();
        String sql = "SELECT * FROM milk_tea_drinks WHERE name LIKE CONCAT('%',?, '%')";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, name);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                MilkTeaDrinks drink = new MilkTeaDrinks();
                drink.setId(resultSet.getInt("id"));
                drink.setName(resultSet.getString("name"));
                drink.setCategory(resultSet.getString("category"));
                drink.setPrice(resultSet.getDouble("price"));
                drink.setDescription(resultSet.getString("description"));
                drinks.add(drink);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return drinks;
    }
}